The goal of this session is to get to started using RStudio, learn how to use variables and solve basic calculations in R. We will cover:
We will be working in pairs:
What to do when getting stuck:
Handing in answers:- Submit to teams assignment page
R is a popular language, especially in data science, this can be seen in the TIOBE Index for August 2020.
It has lots of strengths:
RStudio by default has four main quadrants as shown below. The layout is customisable, as is the background.
When you load RStudio the syntax editor will not be open. Try and open one just like as shown below.
For these workshops we will be using R Markdown. It allows you to have text (with simple formatting) and chunks of R code.
To run code in a code chunk either press the green play button or press Ctrl + Enter (or Cmd + Enter on Mac).
Open the workshop1.rmd file I have sent and save it.
As we will be doing paired programming, each of you should have the file open and take turns to share the screen for each task/exercise. Whoever is screen sharing should copy and paste the code into the chat so that other has it to add to their own document.
We can use R to do simple or advanced calculations for us. Remember to run the code press the green play button or press Ctrl + Enter (or Cmd + Enter on Mac).
## [1] 42
## [1] 1.25
## [1] 21
A variable is a named storage of information. In our case today we are storing numbers.
We can assign variables by using <-. You should see the variable appear to your right in the global enviroment once you’ve run this command.
We can then print the output of the variable by typing in its name.
## [1] 155
When calling a variable, be careful to type it exactly (you can also copy it or use code completion to help).
Try calling the height variable, but spell it incorrectly. You should get an error with something like Error: object ‘hieght’ not found
We can do calculations on these variables, just as we did before. We first assign the variables, then use them in the calculation.
# test scores
Score1 <- 42
Score2 <- 92
Score3 <- 68
# average score calculation
AveScore <- (Score1+Score2+Score3)/3
# print average score
AveScore## [1] 67.33333
You will have noticed the hashtags (#) with text in the above example. These are called comments. In later R sessions we will use a lot of comments to tell us (and others) what each line or section of code is doing.
You can also change the value of a variable you have already assigned. Here we are going to add our new pay check to our previous bank balance. Be sure to run the code.
# create variables
BankBalance <- 100
PayCheck <- 250
# add old bank balance and pay check, assigning result to bank balance
BankBalance <- BankBalance + PayCheck
# print bank balance
BankBalance## [1] 350
Try and add another pay check of £50 to the bank balance variable.
Use R to work out a body mass index (BMI) of someone who is 79kg, and 1.77m tall.
Debug the code below that is finding the weighted average of a students coursework and exam scores. You should find three errors:
# Exercise: weighted average debugging
exam1 <- 52
coursework1 <- 82
exam2 <- 78
coursework2 < 48
cw_weight <- 0.4
ex_weight <- 0.6
course1 <- (exam1 * ex_weight) + (coursework * cw_weight)
course2 <- (exam2 * ex_weight) + (coursework2 * cw_weight)
overall_grade <- (course1 + course2)/3
overall_gradeIt would be very helpful to us to get your feedback. Please can you complete this short survey:
A take home coding task for you, which if you submit to Teams we will mark and give you feedback.
Task: Splitting a Pizza Pilgrims restaurant bill between 3 friends; Roger, Amal and Genevieve.
Take info from Python1 workshop
Recommended for more information on the RStudio environment: https://rladiessydney.org/courses/ryouwithme/01-basicbasics-1/
Recommended for more information on using R Markdown: https://rmarkdown.rstudio.com/lesson-1.html